﻿Public Class BuildData
    Private _LibPath As String = ""
    Private _ProjectFile As String = ""
    Private _OutputFile As String = ""
    ''' <summary>
    ''' Main routine to process the project file
    ''' </summary>
    ''' <param name="ProjectFile">Project filename</param>
    ''' <param name="LibPath">Path to the lib of functions</param>
    ''' <param name="OutputFile">Output file</param>
    ''' <remarks></remarks>
    Public Sub Run(ByVal LibPath As String, ByVal ProjectFile As String, ByVal OutputFile As String)
        Dim fileContents As String

        '==================
        'Store the Lib Path 
        '==================
        Me._LibPath = LibPath
        Me._ProjectFile = ProjectFile
        Me._OutputFile = OutputFile
        '========================
        'Process the Project file
        '========================
        fileContents = ProcessTag(Me._ProjectFile)
        '===============
        'Save the output
        '===============
        My.Computer.FileSystem.WriteAllText(OutputFile, fileContents, False, Text.Encoding.Default)
    End Sub
    Public Function ProcessTag(ByVal FileName As String)
        Dim fileContents As String
        '===============================
        'Load tha file name from the tag
        '===============================
        fileContents = LoadTextFile(FileName)
        '================
        'Extract the tags
        '================
        Dim TagEx As New MPTools.Strings.TagExtractor()
        Dim TItems() As String = TagEx.ExtractTags(fileContents, "<%", "%>", True)
        '======================================
        'Loop throught the tags and process the
        '======================================
        For Each Tobj As String In TItems
            '===============
            'Process the tag
            '===============
            Dim strbuf As String = ProcessTag(IO.Path.Combine(Me._LibPath, Tobj))
            '==========================================
            'Replace the tag with the Processed Content
            '==========================================
            fileContents = fileContents.Replace("<%" & Tobj & "%>", strbuf)
        Next
        '========
        'Clean up
        '========
        TagEx = Nothing
        '==============
        'Return Content
        '==============
        Return fileContents
    End Function
    ''' <summary>
    ''' Load a text file and return as string
    ''' </summary>
    ''' <param name="Filename">Path and file name to text file</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function LoadTextFile(ByVal Filename As String)
        Return My.Computer.FileSystem.ReadAllText(Filename)
    End Function
End Class
